from odoo import http, fields
from odoo.http import request
from ...data_models.api_models import RPInterfaceModelForGet as RP_Get
from ...data_models.base_models import RPBaseModelForAddress as RP_Address
from ...tools.decorator import validate_rp_api, exception_handler
from ..abstract_interface.personal import APIPersonal
from ...tools.encapsulation import valid_response, invalid_business, invalid_response
from ...static import request_schema_json


class APIPersonalImplement(APIPersonal):

    @exception_handler
    @validate_rp_api(schema=request_schema_json.get_address_list, lang=True, token=True)
    @http.route(route='/api/personal/get_address_list', auth='none', methods=['GET'], cors="*", csrf=False)
    def get_address_list(self, **kwargs):
        """ 获取地址列表 """
        _customer_id = request.customerid
        _language_code = request.lang
        rp_get = RP_Get(kwargs.get('page', 1), kwargs.get('limit', 10))
        address_rcds = request.env['res.partner'].sudo().with_context({'lang': _language_code}).search([('customer_address_id', '=', _customer_id)],
                                                                                                       offset=rp_get.offset, limit=rp_get.limit)
        data = [RP_Address(env=request.env, record=i).printf() for i in address_rcds]
        return valid_response(data)

    @exception_handler
    @validate_rp_api(schema=request_schema_json.create_address, token=True)
    @http.route(route='/api/personal/create_address', type='http', auth='none', methods=['POST'], cors="*", csrf=False)
    def create_address(self, **kwargs):
        """ 创建地址  """
        _customer_id = request.customerid

        _first_name = kwargs.get('first_name')
        _last_name = kwargs.get('last_name')
        _mobile = kwargs.get('mobile')
        _company = kwargs.get('company', '')
        _postal_code_id = kwargs.get('postal_code_id')
        _country_id = kwargs.get('country_id')
        _state_id = kwargs.get('state_id', None)
        _city_id = kwargs.get('city_id')
        _county_id = kwargs.get('county_id', None)
        _street = kwargs.get('street')

        _default = kwargs.get('default', False)

        rp_address = RP_Address(env=request.env, customer_id=_customer_id,
                                country_id=_country_id, state_id=_state_id, city_id=_city_id, county_id=_county_id, postal_code_id=_postal_code_id, street=_street,
                                first_name=_first_name, last_name=_last_name, mobile=_mobile, default=_default, company=_company)
        data = {'id': rp_address.create()}
        return valid_response(data)

    @exception_handler
    @validate_rp_api(schema=request_schema_json.update_address, token=True)
    @http.route(route='/api/personal/update_address', auth='none', methods=['PUT'], cors="*", csrf=False)
    def update_address(self, **kwargs):
        """ 修改地址 """
        _customer_id = request.customerid
        _id = kwargs.get('id')

        _first_name = kwargs.get('first_name')
        _last_name = kwargs.get('last_name')
        _mobile = kwargs.get('mobile')
        _company = kwargs.get('company', '')
        _postal_code_id = kwargs.get('postal_code_id')
        _country_id = kwargs.get('country_id')
        _state_id = kwargs.get('state_id', None)
        _city_id = kwargs.get('city_id')
        _county_id = kwargs.get('county_id', None)
        _street = kwargs.get('street')

        _default = kwargs.get('default', False)

        rp_address = RP_Address(env=request.env, id=_id, customer_id=_customer_id,
                                country_id=_country_id, state_id=_state_id, city_id=_city_id, county_id=_county_id, postal_code_id=_postal_code_id, street=_street,
                                first_name=_first_name, last_name=_last_name, mobile=_mobile, default=_default, company=_company)
        data = {'data': rp_address.write()}
        return valid_response(data)

    @exception_handler
    @validate_rp_api(schema=request_schema_json.unlink_address, token=True)
    @http.route(route='/api/personal/unlink_address', auth='none', methods=['DELETE'], cors="*", csrf=False)
    def unlink_address(self, **kwargs):
        """ 删除地址 """
        _customer_id = request.customerid
        _ids = kwargs.get('ids')
        flag = False
        for id in _ids:
            rp_address = RP_Address(env=request.env, id=id)
            flag = rp_address.unlink()
        data = {'data': flag}
        return valid_response(data)
